package com.xiaomai.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaomai.commonutil.R;
import com.xiaomai.eduservice.entity.EduTeacher;
import com.xiaomai.eduservice.entity.vo.TeacherQuery;
import com.xiaomai.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-08-20
 */
@Api(tags={"讲师管理"})
@RestController
@RequestMapping("/eduservice/edu-teacher")

public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    //查询操作
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }
    //删除操作
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById(	@ApiParam(name = "id", value = "讲师ID", required = true)@PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        if (flag) {
           return R.ok();
        } else {
           return R.error();
        }
     }
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageTeacher/{page}/{limit}")
    public R pageListTeacher( @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
                       @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit){
        Page<EduTeacher> pageTeacher = new Page<>(page,limit);
         eduTeacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("records",records);
    }
    //条件分页查询
    @ApiOperation(value = "条件分页查询")
    @PostMapping("pageTeacherCondition/{page}/{limit}")
    public R pageTeacherCondition(@PathVariable Long page, @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //分页条件
        Page<EduTeacher> pageTeacher = new Page<>(page,limit);
        //查询条件
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }else if(!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }else if(!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }else if(!StringUtils.isEmpty(begin)){
            queryWrapper.le("gmt_modified",begin);
        }
        queryWrapper.orderByDesc("gmt_modified");
        eduTeacherService.page(pageTeacher,queryWrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    //添加讲师
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher  eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag){
          return  R.ok();
        }else {
         return  R.error();
        }

    }

    //根据id查找讲师
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }
    //根据id修改讲师
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

